package com.zlj.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter("/admin/*")
public class AdminFilter implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

		/*
		 * 1、得打session
		 * 2、判断session中是否存在admin:如果存在，则放行
		 * 3、判断session中是否存在user：如果存在则放行，如果不存在，则返回Login.jsp，并告诉她让他先登录
		 * 
		 */
		HttpServletRequest req = (HttpServletRequest) request;
		
		String admin = (String)req.getSession().getAttribute("admin");
		
		if(admin != null) {
			chain.doFilter(request, response);
			return;
		} else {
			req.setAttribute("msg", "您不是管理员，请先去登录！");
		}
		
		request.getRequestDispatcher("/login.jsp").forward(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}
}
